import player from "@/game/player";
import { Player } from "@/types/game/player";
import { defineStore } from "pinia";
import { computed, reactive, ref } from "vue";
const useGameStore = defineStore("game", () => {
    const roundNumber = ref(0);
    const gamePaused = ref<boolean>(false);
    const gameStarted = ref<boolean>(false);
    const gameEnded = ref<boolean>(false);
    const playerStates = reactive({
        playerList: [] as Player[],
        currentPlayerId: null as string | null,
        currentPlayer: computed<Player>(() => playerStates.playerList.find((player: Player) => player.id === playerStates.currentPlayerId)),
        currentPlayerSeat: computed<number>(() => (<Player>playerStates.currentPlayer).seat)
    })
    function setPlayerList(list: Player[]) {
        playerStates.playerList = list;
    }
    function removePlayer(criterion: Player | string | ((playerItem: Player) => boolean)) {
        if (typeof criterion === "string") playerStates.playerList = playerStates.playerList.filter((playerItem: Player) => playerItem.name !== criterion)
        if (typeof criterion === "function") playerStates.playerList = playerStates.playerList.filter(criterion)
        if (typeof criterion === "object") playerStates.playerList = playerStates.playerList.filter((playerItem: Player) => playerItem !== criterion)
    }
    function addPlayer(name?: string, seat?: number) {
        if (!name) name = player.randomGetCountryName((<Player[]>playerStates.playerList).map(playerItem => playerItem.name));
        const playerItem = player.createPlayer(name, seat)
        playerStates.playerList.push(playerItem);
        return playerItem;
    }
    const init = () => {
        roundNumber.value = 0;
        gameStarted.value = false;
        gameEnded.value = false;
        gamePaused.value = false;
        playerStates.playerList = [];
        playerStates.currentPlayerId = null;
    }
    return {
        roundNumber,
        gameStarted,
        gameEnded,
        gamePaused,
        playerStates,
        $action: {
            init,
            playerManagement: {
                setPlayerList,
                addPlayer,
                removePlayer,
            }
        }
    };
})
export default useGameStore;